/*
 * Created by JFormDesigner on Wed Jul 08 19:23:33 CST 2020
 */
package entity.JFrame;

import entity.JPanel.Book_type;
import entity.JPanel.SearchBook;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class DetailJFrame extends JFrame {
    private Random random = new Random();
    private Color colors[] = {new Color(238, 130, 238), new Color(123, 104, 238), new Color(0, 191, 255), new Color(255, 140, 0),
            new Color(0, 250, 154), new Color(173, 255, 47), new Color(255, 255, 0), new Color(240, 128, 128), new Color(255, 20, 147)};
    private JLabel welcome;
    //创建修改密码按钮
    private JButton jButton_toobar[] = {new JButton("修改密码"), new JButton("图书管理"), new JButton("搜索图书")};
    private int rand[] = new int[jButton_toobar.length];
    //内容面板，其上用于添加其他待切换的面板
    private JPanel contentPanel;
    //初始化选项卡面板
    private JTabbedPane tabPane = new JTabbedPane();
    //创建工具条
    private JToolBar jToolBar = new JToolBar();
    BufferedImage bg;

    public DetailJFrame() {
        setVisible(true);
        setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height);
        //窗口居中
        setLocationRelativeTo(null);
        //设置窗口最大化
        setExtendedState(MAXIMIZED_BOTH);
        //设置标题
        setTitle("详细");
        //设置布局为边界布局管理器。将contentPanel添加到窗口中心位置。
        initComponents();
    }

    public static void main(String[] args) {
        new DetailJFrame();
    }

    private void initComponents() {
        //========jToolBar======
        jToolBar.setRollover(true);
        for (int i = 0; i < jButton_toobar.length; i++) {
            //jButton_toobar[i].setBackground(colors[random.nextInt(colors.length)]);
            jToolBar.add(jButton_toobar[i]);
        }
        /**
         *
         */
        for (int i = 0; i < jButton_toobar.length; i++) {//外循环用于获取随机数0-4
            rand[i] = new Random().nextInt(colors.length);//获取随机数并放入数组中
            for (int j = 0; j < i; j++) {//用于判断是否有相同的随机数
                if (rand[i] == rand[j]) {//若果相同，本次的随机数即从新获取
                    i--;
                    break;
                }
            }
        }
        for (int i = 0; i < rand.length; i++) {
            System.out.println(rand[i]);
            jButton_toobar[i].setBackground(colors[rand[i]]);
        }
        jButton_toobar[0].addActionListener((e) -> {
            //showSpecifiedPanel(contentPanel,new ResetPasswordJPanel());
            System.out.println("修改密码");
        });
        jButton_toobar[1].addActionListener((e) -> {
            showSpecifiedPanel(contentPanel, new Book_type());
            System.out.println("图书管理");
        });
        jButton_toobar[2].addActionListener((e) -> {
            showSpecifiedPanel(contentPanel, new SearchBook(1000, 800));
            System.out.println("搜索图书");
        });
        //======== this ========
        setLayout(new BorderLayout());
        add(jToolBar, BorderLayout.NORTH);
        contentPanel = new JPanel();
        add(contentPanel, BorderLayout.CENTER);
        Font font = new Font("楷体·", Font.PLAIN, 30);//定义
        welcome = new JLabel("欢迎来到图书管理系统");
        welcome.setIcon(new ImageIcon("C:\\Users\\429762125\\Desktop\\\u5c0f\u7a0b\u5e8f\u56fe\u7247\\\u5730\u7406.png"));
        welcome.setOpaque(true); //此句是重点，设置背景颜色必须先将它设置为不透明的，因为默认是透明的。
        welcome.setBackground(Color.GRAY);//设置背景颜色为red。
        welcome.setForeground(Color.cyan);//设置字体颜色为black。
        welcome.setFont(font);//设置字体和大小。
        contentPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 50));// 向左对齐,水平间距是20，垂直间距为40像素
        contentPanel.add(welcome);
    }
    // 以contentPanel为底，其上覆盖想要展示的panel内容(切换panel)。

    /**
     * @param contentPanel 被替换的面板
     * @param showPanel    展示的面板
     */
    private void showSpecifiedPanel(JPanel contentPanel, JPanel showPanel) {
        contentPanel.removeAll();
        System.out.println(contentPanel.getLayout());
        contentPanel.add(showPanel);
        contentPanel.validate();
        contentPanel.repaint();
    }
}

